package com.cyt.work.enrollment.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.enrollment.dao.EnrollmentTestDao;
import com.cyt.work.enrollment.model.po.EnrollmentTest;
import com.cyt.work.enrollment.model.vo.EnrollmentTestVo;
import com.cyt.work.enrollment.service.EnrollmentTestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 自主招生-学生考试成绩Service层实现类
 *
 * @author caideyong
 * @date 2019-11-22 16:38:52
 */
@Service("enrollmentTestService")
public class EnrollmentTestServiceImpl extends BaseServiceImpl<EnrollmentTest> implements EnrollmentTestService {

    @Resource
    private EnrollmentTestDao enrollmentTestDao;

    @Autowired
    public void setBaseDao(EnrollmentTestDao enrollmentTestDao) {
        this.baseDao = enrollmentTestDao;
    }

    /**
     * 查询对象集合
     *
     * @param enrollmentTestVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-11-22 16:38:52
     */
    @Override
    public List<EnrollmentTest> listExample(EnrollmentTestVo enrollmentTestVo) {
        Example example = new Example(EnrollmentTest.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if(StringUtils.isNotBlank(enrollmentTestVo.getStuinfoId())){
            criteria.andEqualTo("stuinfoId", enrollmentTestVo.getStuinfoId());
        }
        return enrollmentTestDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param enrollmentTestVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:38:52
     */
    @Override
    public ReturnResult page(EnrollmentTestVo enrollmentTestVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<EnrollmentTest> list = listExample(enrollmentTestVo);
        PageInfo<EnrollmentTest> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param enrollmentTestVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:38:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(EnrollmentTestVo enrollmentTestVo, SysUser sysUser) {
        //查询主键id是否存在,不存在就新建,存在就更新
        if (StringUtils.isBlank(enrollmentTestVo.getId())) {
            EnrollmentTest enrollmentTest = new EnrollmentTest(sysUser);
            enrollmentTest.setName(enrollmentTestVo.getName());
            enrollmentTest.setChinese(enrollmentTestVo.getChinese());
            enrollmentTest.setMath(enrollmentTestVo.getMath());
            enrollmentTest.setEnglish(enrollmentTestVo.getEnglish());
            enrollmentTest.setPhysical(enrollmentTestVo.getPhysical());
            enrollmentTest.setChemistry(enrollmentTestVo.getChemistry());
            enrollmentTest.setPoliticial(enrollmentTestVo.getPoliticial());
            enrollmentTest.setHistory(enrollmentTestVo.getHistory());
            enrollmentTest.setTotalScore(enrollmentTestVo.getTotalScore());
            enrollmentTest.setClassRank(enrollmentTestVo.getClassRank());
            enrollmentTest.setGradeRank(enrollmentTestVo.getGradeRank());
            enrollmentTest.setStuinfoId(enrollmentTestVo.getStuinfoId());
            enrollmentTest.setCreator(enrollmentTestVo.getCreator());
            enrollmentTest.setUpdator(enrollmentTestVo.getUpdator());
            enrollmentTestDao.insert(enrollmentTest);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            EnrollmentTest enrollmentTest = enrollmentTestDao.selectByPrimaryKey(enrollmentTestVo.getId());
            enrollmentTest.setName(enrollmentTestVo.getName());
            enrollmentTest.setChinese(enrollmentTestVo.getChinese());
            enrollmentTest.setMath(enrollmentTestVo.getMath());
            enrollmentTest.setEnglish(enrollmentTestVo.getEnglish());
            enrollmentTest.setPhysical(enrollmentTestVo.getPhysical());
            enrollmentTest.setChemistry(enrollmentTestVo.getChemistry());
            enrollmentTest.setPoliticial(enrollmentTestVo.getPoliticial());
            enrollmentTest.setHistory(enrollmentTestVo.getHistory());
            enrollmentTest.setTotalScore(enrollmentTestVo.getTotalScore());
            enrollmentTest.setClassRank(enrollmentTestVo.getClassRank());
            enrollmentTest.setGradeRank(enrollmentTestVo.getGradeRank());
            enrollmentTest.setStuinfoId(enrollmentTestVo.getStuinfoId());
            enrollmentTest.setCreator(enrollmentTestVo.getCreator());
            enrollmentTest.setUpdator(enrollmentTestVo.getUpdator());
            enrollmentTest.setUpdateTime(new Date());
            enrollmentTest.setUpdator(sysUser.getId());
            enrollmentTestDao.updateByPrimaryKey(enrollmentTest);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param enrollmentTestVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:38:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(EnrollmentTestVo enrollmentTestVo, SysUser sysUser) {
        //要修改的字段
        EnrollmentTest enrollmentTest = new EnrollmentTest();
        enrollmentTest.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        enrollmentTest.setUpdator(sysUser.getId());
        enrollmentTest.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(EnrollmentTest.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(enrollmentTestVo.getId())) {
            criteria.andIn("id", Arrays.asList(enrollmentTestVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        enrollmentTestDao.updateByExampleSelective(enrollmentTest, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param enrollmentTestVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:38:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(EnrollmentTestVo enrollmentTestVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(EnrollmentTest.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(enrollmentTestVo.getId())) {
            criteria.andIn("id", Arrays.asList(enrollmentTestVo.getId().split(",")));
        }
        enrollmentTestDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public void insertList(List<EnrollmentTest> enrollmentTestList) {
        enrollmentTestDao.insertList(enrollmentTestList);
    }
}